<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 菲华2 ajie
 */
class Feihua2Controller extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.ordNo") ? I("request.ordNo") : I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        // $isMobile = isMobile();
        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //微信
            case 1:
                /*$pay_bankcode = '1';
                break;*/
            //微信H5
            case 2:
                $pay_bankcode = '3';
                break;

            //支付宝
            case 3:
            case 17: //原生
            case 4:
            case 18: //原生
                $pay_bankcode = '2';
                break;
            //支付宝H5
            case 4:
            case 18: //原生
                $pay_bankcode = '4';
                break;
            //银联扫码
            case 11:
            case 19:
            case 9:
                $pay_bankcode = '5';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $native = array(
            "mchid" => $this->config['mch_id'],
            "order_id" => $return['orderid'] ,
            "total_amount" => intval($return['amount']),
            "channel_id" => $pay_bankcode ,
            "return_url" => $return['notifyurl'],
        );


        // 对请求参数进行重新的排序
        ksort( $native, SORT_NATURAL | SORT_FLAG_CASE ) ;
        // 服务端连接字符串
        $serverStr = "" ;
        // 拼接连接字符串
        foreach ( $native as $k => $v ) {
            $serverStr = $serverStr . $k . $v ;
        }
        $serverStr = $serverStr . $this->config['signkey'] ;
        $native['sign'] = strtoupper( sha1( $serverStr ) ) ;


        $res = curl($this->config['gateway'], httpstr($native));

        //var_dump('<pre/>',$serverStr,$native,$this->config['gateway'],$this->config['signkey'],$res);exit();

        $resp = json_decode($res,true);

        $code = $resp['url'];

        if(!$code){
            if($resp['msg']){
                $this->showmessage($resp['msg'],['code'=>C("THIRDCODES.".$this->class)]);
            }

            $this->showmessage('获取支付连接失败！',['code'=>C("THIRDCODES.".$this->class)]);
        }

        header("Location: {$code}");

    }



    //通知
    public function notifyurl(){
        //$this->debug_callback();

        //IP白名单
        if(!$this->checkip(getIP(),['45.32.56.218'])){
            exit("wrong");
        }

        //$p = json_decode(file_get_contents('php://input'),true);
        $p = I("post.");

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['order_id'];

        //获取订单
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
             "pay_orderid_rd"=>htmlspecialchars($p['order_no'])
        ));


        $real_amount =  $p['total_amount'];     //实际支付金额
        $pay_amount =  $find['pay_amount']; //商户提交订单金额

        if(abs($real_amount - $pay_amount) > 1){
            echo 'wrong_money'; exit;
        }

        //获取渠道信息
        $this->config = Channel::getInstance()->find(['code'=>$this->class]);

        $sign = $p['sign'];


        $returnParams	= [
            'mchid'			=> $p['mchid'] ,		// 商户接入编号
            'order_id'		=> $p['order_id'] ,		// 商户订单号
            'order_no'		=> $p['order_no'] ,		// 平台订单号
            'channel_id'	=> $p['channel_id'] ,	// 通道编号
            'total_amount'	=> $p['total_amount'] ,	// 订单总额
        ] ;
        ksort( $returnParams, SORT_NATURAL | SORT_FLAG_CASE ) ;

        // 服务端连接字符串
        $serverStr  = "" ;

        // 拼接连接字符串
        foreach ( $returnParams as $k => $v ) {
            $serverStr  = $serverStr . $k . $v ;
        }

        $serverStr  = $serverStr . $this->config['signkey'] ;
        $sign2 = strtoupper( sha1( $serverStr ) ) ;

        if($sign !== $sign2){
            exit("sign_wrong");
        }


        $this->async($find['pay_orderid'],"success");
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}
